/*
 * @ auth hdg @ email hdg1988@gmail.com @ date 弹出窗口------超类
 * @ modify huangpf @ hpf1908@gmail.com @ 20100414
 */

//组件管理对象
var compManager={
	push:function(_ID,_OBJ){
		if(!this[_ID]){
			this[_ID]=_OBJ;
		}
	},
	getCmpById:function(_ID){
		if(this[_ID]){
			return this[_ID];
		}
		else
			return null;
	}
};

function BasePanel(config) {

	//基类窗体的参数
	if(config){
		var props = {
		id:config.id,                    // 窗口id
		titleText:config.titleText,      // 标题说明文字
		titleImg:config.titleImg,         // 标题图标
		left:50,
		top:150,
		width: 250,
		height: 400,
		lastElmX :0,
  	lastElmY :0,
  	lastElmW :0,
  	lastElmH :0,
  	status:'normal',
  	OnMin:null,
  	OnMax:null,
  	OnRestore:null,
  	OnClose:null,
  	OnMouseDown:null
  };

  for (var p in props)
   this[p] = (typeof config[p] == 'undefined') ? props[p] : config[p];	
	}
	
	// 窗体div
	this.panelTemplate = "<div id='{0}_panel' "+
													"class='panel drsElement' "+
													"style='left: {1}px; top: {2}px; width: {3}px; height: {4}px;' >"+
												"</div>";

	// 窗体头部标题栏
	this.headTemplate = ["<div class='panel_head drsMoveHandle l'>", "<div class='r'>",
			"<div class='c'></div>", "</div>", "</div>"].join("");		

	this.headContent = ["<div class='head_image'><img src='{0}'/></div>",
			"<div class='head_title'>{1}</div>",
			"<div class='head_tools'>",
				"<div panelid='{2}' class='minBtn' title='最小化'></div>",
				"<div panelid='{3}' class='maxBtn' title='最大化'></div>",
				"<div panelid='{4}' class='restoreBtn' title='向下还原'></div>",
				"<div panelid='{5}' class='closeBtn' title='关闭'></div>",
			"</div>"].join("");

	// 窗体中间区域
	this.bodyTemplate = ["<div class='panel_body l'>", "<div class='r'>",
			"<div class='c'></div>", "</div>", "</div>"].join("");

	// 窗体底部
	this.bottomTemplate = ["<div class='panel_bottom l'>", "<div class='r'>",
			"<div class='c'></div>", "</div>", "</div>"].join("");
			
	//组装参数
	this.panelTemplate=$format(this.panelTemplate,	[this.id,this.left,this.top,this.width,this.height]);
	this.headContent=$format(this.headContent,[this.titleImg,this.titleText,
									this.id,this.id,this.id,this.id]);
	
	//添加到组件管理者							
	compManager.push(this.id,this);
};

BasePanel.prototype.init=function() {		
		// 添加窗体div
		
		$(this.panelTemplate).appendTo("body");

		// 添加窗体标题栏
		$(this.headTemplate).appendTo("#" + this.id + "_panel");
		$(this.headContent).appendTo("#" + this.id + "_panel .panel_head .c");

		// 添加body
		$(this.bodyTemplate).appendTo("#" + this.id + "_panel");
		// 添加窗体底部
		$(this.bottomTemplate).appendTo("#" + this.id + "_panel");
		
		//进行事件绑定
		this.eventBind();
		$("#" + this.id + "_panel").hide();
};

BasePanel.prototype.restorePanel=function(){
	
	var element=document.getElementById(this.id+"_panel");
  element.style.zIndex = ++dragresize.zIndex;
  $(element).animate({
	left:this.lastElmX + 'px',
	width:this.lastElmW + 'px',
	top:this.lastElmY + 'px',
	height: this.lastElmH + 'px'
  });
	/*with (element.style)
 	{
  	left =   this.lastElmX + 'px';
  	width =  this.lastElmW + 'px';
  	top =    this.lastElmY + 'px';
  	height = this.lastElmH + 'px';
 	}*/
}

BasePanel.prototype.maxPanel=function(){
	
	var element=document.getElementById(this.id+"_panel");
	element.style.zIndex = ++dragresize.zIndex;
	$(element).animate({
		left:'0px',
		width:$(window).width() + 'px',
		top:0 + 'px',
		height:$("#desktop").height()-34 + 'px'
	});
	/*with (element.style)
 	{
  	left =   0 + 'px';
  	width =  $(window).width() + 'px';
  	top =    0 + 'px';
  	height = $(window).height()-50 + 'px';
 	}*/
}
BasePanel.prototype.minPanel=function(){
	var windowTask = $('#windowTaskManager_'+this.id);
	//$('#'+this.id+"_panel").hide();
	$('#'+this.id+"_panel").animate({
		left:windowTask.offset().left,
		top:windowTask.offset().top,
		width:windowTask.width(),
		height:windowTask.height()
	},{complete:function(){
			$('#'+this.id).hide();
	}});
	
}
//需要和dragResize进行交互
BasePanel.prototype.headToolsMouseDown=function(){
	//获得窗体id
	var id=$(this).attr('panelid');
	var elmPanel=$("#"+id+"_panel");
	if(elmPanel!=null){
		var panel=compManager.getCmpById(id);
		var element=elmPanel.get(0);
		
		//点击最小化
		if($(this).attr('class')=='minBtn'){
			if (panel.OnMin) panel.OnMin();
			panel.status='minMum';
			panel.lastElmX = parseInt(element.style.left);
			panel.lastElmY = parseInt(element.style.top);
			panel.lastElmW = element.offsetWidth;
			panel.lastElmH = element.offsetHeight;
			//elmPanel.hide();
			panel.minPanel();
		}
		
		//点击最大化,更改class属性
		if($(this).attr('class')=='maxBtn'){
			$(this).hide();
			$("#"+id+"_panel .head_tools .restoreBtn").show();
			panel.lastElmX = parseInt(element.style.left);
  		panel.lastElmY = parseInt(element.style.top);
  		panel.lastElmW = element.offsetWidth;
  		panel.lastElmH = element.offsetHeight;
  		panel.maxPanel();
  		panel.status='maxMum';
  		if (panel.OnMax) panel.OnMax();
		}
		
		//点击还原,更改class属性
		if($(this).attr('class')=='restoreBtn'){
			//根据原来的x,y,w,h进行还原
			$(this).hide();
			$("#"+id+"_panel .head_tools .maxBtn").show();
			panel.restorePanel();
			panel.status='normal';
			if (panel.OnRestore) panel.OnRestore();
		}
		
		if($(this).attr('class')=='closeBtn'){
			if (panel.OnClose) {
				panel.OnClose();
			}
		}
	}
}

BasePanel.prototype.eventBind=function() {		
	$("#"+this.id+"_panel .head_tools .minBtn").bind("mousedown",BasePanel.prototype.headToolsMouseDown);
	$("#"+this.id+"_panel .head_tools .maxBtn").bind("mousedown",BasePanel.prototype.headToolsMouseDown);
	$("#"+this.id+"_panel .head_tools .closeBtn").bind("mousedown",BasePanel.prototype.headToolsMouseDown);
	$("#"+this.id+"_panel .head_tools .restoreBtn").bind("mousedown",BasePanel.prototype.headToolsMouseDown);

	$("#"+this.id+"_panel").bind("mousedown",BasePanel.prototype.mouseDown);	
	//阻止冒泡
	stopElemEvent("mousedown","#"+this.id+"_panel .head_tools");
};

BasePanel.prototype.mouseDown=function() {	
	var temp=this.id.split('_');
	var panel=compManager.getCmpById(temp[0]);
	if(panel.OnMouseDown!=null)
		panel.OnMouseDown();
};

BasePanel.prototype.setStyle=function() {		

};

function NoteBookPanel(config) {
	BasePanel.call(this, config);
	
	this.testAreaTpl=["<div class='divTextArea'>", 
	                    "<div id='{0}' name='{1}' class='editTextArea'>","</div>",	                     
		                "</div>"].join("");
	
};

NoteBookPanel.prototype=new BasePanel(); 
NoteBookPanel.prototype.constructor=NoteBookPanel;

NoteBookPanel.prototype.initMenus=function(renderid){
	
	var menus=new noteBookMenu(this.id+'_menu');
	menus.renderTo=renderid;
	menus.reg([{
					key:'my_doc',
					desc:'文件(F)',
					title:'文件(F)',
					position:'right',
					imgSrc:'image/my_documents.png',
					cmd:function(){
						alert("我的文档")
					},
					items:[{
									key:"fadajian",
									desc:"新建",
									title:"新建",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"打开",
									title:"打开",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"保存",
									title:"保存",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"另存为",
									title:"另存为",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"页面设置",
									title:"页面设置",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"打印",
									title:"打印",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"退出",
									title:"退出",
									imgSrc:''
							}]
				},{
					key:'my_doc',
					desc:'编辑(E)',
					title:'编辑(E)',
					position:'right',
					imgSrc:'image/my_documents.png',
					cmd:function(){
						alert("我的文档")
					},
					items:[{
									key:"fadajian",
									desc:"撤销",
									title:"撤销",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"剪切",
									title:"剪切",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"复制",
									title:"复制",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"粘贴",
									title:"粘贴",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"删除",
									title:"删除",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"查找",
									title:"查找",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"查找下一个",
									title:"查找下一个",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"替换",
									title:"替换",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"转到",
									title:"转到",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"全选",
									title:"全选",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"时间/日期",
									title:"时间/日期",
									imgSrc:''
							}]
				},{
					key:'my_doc',
					desc:'格式(O)',
					title:'格式(O)',
					position:'right',
					imgSrc:'image/my_documents.png',
					cmd:function(){
						alert("我的文档")
					},
					items:[{
									key:"fadajian",
									desc:"自动换行",
									title:"自动换行",
									imgSrc:''
							},{
									key:"fadajian",
									desc:"字体",
									title:"字体",
									imgSrc:''
							}]
				},{
					key:'my_doc',
					desc:'查看(V)',
					title:'查看(V)',
					position:'right',
					imgSrc:'image/my_documents.png',
					cmd:function(){
						alert("我的文档")
					},
					items:[{
									key:"fadajian",
									desc:"状态栏",
									title:"状态栏",
									imgSrc:''
							}]
				},{
					key:'my_doc',
					desc:'帮助(H)',
					title:'帮助(H)',
					position:'right',
					imgSrc:'image/my_documents.png',
					cmd:function(){
						alert("我的文档")
					},
					items:[{
									key:"fadajian",
									desc:"帮助主题",
									title:"帮助主题",
									imgSrc:''
							},{
									key:'seperate',
									position:'right'
							},{
									key:"fadajian",
									desc:"关于记事本",
									title:"关于记事本",
									imgSrc:''
							}]
				}]);
}

NoteBookPanel.prototype.init=function(){
	BasePanel.prototype.init.call(this);
	
	this.initMenus(this.id + "_panel .panel_body .c");
	this.testAreaTpl=$format(this.testAreaTpl,['id-'+this.id,'name-'+this.id]);
	
	$(this.testAreaTpl).appendTo("#" + this.id + "_panel .panel_body .c");
	
	$('#id-'+this.id).get(0).contentEditable = true;
	
	//var testAreaHeight=$("#"+this.id+"_panel .panel_body .c").height();
	//$("#"+this.id+"_panel .c .divTextArea").height(testAreaHeight-19);
	document.execCommand('2D-Position', true, true);  
};
